<?php
class Zendme_Models_NewsItem extends Zend_Db_Table{
	protected $_name = 'news_item';
	protected $_primary = 'id';
	
	public function countItem($arrParam, $options = null){		
		if($options['task'] == 'public-index'){
			$db	= $this->getAdapter();
			$ssFilter = $arrParam['ssFilter'];
			$select = $db->select()
						 ->from('news_item as p',array('COUNT(p.id) AS totalItem'))
						 ->where('p.status = 1');
			if(!empty($ssFilter['keySearch'])){
				$keywords = '%' . $ssFilter['keySearch'] . '%';
				$select->where('p.name LIKE ?',$keywords,STRING);
			}
			$result = $db->fetchOne($select);
		}
		
		if($options['task'] == 'public-category'){
			$db	= $this->getAdapter();
			
			$select=$db->select()
						 ->from('news_category',array('id'))
						 ->where('slug=?',$arrParam['slug']);
						 
			$cat_id=$db->fetchOne($select);
							
			$select = $db->select()
						 ->from('news_item as p',array('COUNT(p.id) AS totalItem'))
						 ->where('p.status = 1')
						 ->where('cat_id=?',$cat_id);
			$result = $db->fetchOne($select);
		}
		
		return $result;					 
	}
	
	public function listItem($arrParam = null, $options = null){
		
		if($options['task'] == 'public-index'){
			$ssFilter = $arrParam['ssFilter'];
			$paginator = $arrParam['paginator'];
			
			$db	= $this->getAdapter();
			$select = $db	->select()
							->from("news_item AS p",array('id','name','summary','content','cat_id','special','picture'))
							->joinLeft('news_category AS c','c.id = p.cat_id',array('c.name AS category_name'))
							->where('p.status = 1')
							->order(' id DESC')
							->limitPage($paginator['currentPage'],$paginator['itemCountPerPage']);
			if(!empty($ssFilter['keySearch'])){
				$keywords = '%' . $ssFilter['keySearch'] . '%';
				$select->where('p.name LIKE ?',$keywords,STRING);
			}				
			
			$result = $db->fetchAll($select);
		}
		if($options['task'] == 'public-category')
		{
			$db=$this->getAdapter();
			
			$select=$db->select()->from('news_category',array('id'))
								 ->where('slug=?',$arrParam['slug']);
			$cat_id=$db->fetchOne($select);

			$select=$this->select()
					   	 ->where('cat_id=?',$cat_id)
					   	 ->where('status=1')
					   	 ->limitPage($options['paginator']['currentPage'], $options['paginator']['itemCountPerPage']);
			$result=$this->fetchAll($select)->toArray();
		}
				
		if($options['task']=='public-news-slide')
		{
			$select=$this->select()
						 ->where('special=?',1)
						 ->where('status=1')
						 ->order('id desc')
						 ->limit(5);
			$result=$this->fetchAll($select)->toArray();			 
		}
		
		if($options['task']=='public-news-feature')
		{
			$select=$this->select()	
						 ->order('id desc')
						 ->where('feature=1');
			$result=$this->fetchAll($select)->toArray();			 
		}
		
		if($options['task']=='public-news-home')
		{
			$select=$this->select()
						 ->where('feature=1')
						 ->where('status=1')
						 ->limit(4);
			$result=$this->fetchAll($select)->toArray();			 
		}
		
		if($options['task']=='public-news-related')
		{
			$select=$this->select()
						 ->where('status=1')
						 ->where('slug!=?',$arrParam['slug'])
						 ->order('rand()')
						 ->limit(5);
						 
			$result=$this->fetchAll($select);			 
		}
		
		return $result;
	}
	
	
	public function getItem($arrParam, $options = null){		
		if($options['task'] == 'public-detail'){
			$select = $this->select()
						   ->where('slug = ?',$arrParam['slug']);
			$result = $this->fetchRow($select)->toArray();
		}		
		return $result;
	}
}